package com.jk.api.controller;


import com.jk.api.dto.FinishDTO;
import com.jk.api.enums.TypeEnums;
import com.jk.api.service.ApiService;
import com.jk.common.annotation.KrtIgnoreAuth;
import com.jk.common.bean.ReturnBean;
import com.jk.common.bean.ReturnCode;
import com.jk.common.exception.KqException;
import com.jk.kq.entity.LeaveRecord;
import com.jk.kq.entity.vo.LeaveRecordVO;
import com.jk.kq.entity.vo.WriteoffRecordVo;
import com.jk.kq.service.ILeaveRecordService;
import com.jk.kq.service.IWorkTypeService;
import com.jk.kq.service.IWriteoffRecordService;
import com.jk.sys.entity.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/wechat/kq")
@Slf4j
public class FinishApiController {


    @Autowired
    private ILeaveRecordService leaveRecordService;

    @Autowired
    private IWorkTypeService workTypeService;

    @Autowired
    private IWriteoffRecordService writeoffRecordService;

    @Autowired
    private ApiService apiService;


    @KrtIgnoreAuth
    @GetMapping("finish/list")
    public ReturnBean applicationList(HttpServletRequest request, FinishDTO dto) throws KqException {
        User openUser = apiService.getOpenUser(request);
        Map<String,Object> map = new HashMap<>();
        if (ObjectUtils.isEmpty(dto.getTypeId())){
            return ReturnBean.error(
                    ReturnCode.INVALID_REQUEST.getCode(),"类型id为空"
            );
        }
        if (ObjectUtils.isEmpty(dto.getDate())){
            return ReturnBean.error(
                    ReturnCode.INVALID_REQUEST.getCode(),"日期为空"
            );
        }
        map.put("date",dto.getDate());
        //默认查请假
        Integer typeId = dto.getTypeId();
        if (TypeEnums.LEAVE.getStatus().equals(typeId)){
            map.put("type",TypeEnums.LEAVE.name().toLowerCase());
            List<LeaveRecordVO> doneTasks = leaveRecordService.findDoneTasks(map, openUser.getName());
            for (LeaveRecordVO doneTask : doneTasks) {
                Integer typeIdDb = doneTask.getTypeId();
                doneTask.setTypeName(workTypeService.selectById(typeIdDb).getName());
            }
            return ReturnBean.ok(doneTasks);
        }else if (TypeEnums.WRITEOFF.getStatus().equals(typeId)){
            map.put("type",TypeEnums.WRITEOFF.name().toLowerCase());
            List<WriteoffRecordVo> doneTasks = writeoffRecordService.findDoneTasks(map, openUser.getName());
            for (WriteoffRecordVo doneTask : doneTasks) {
                Integer typeIdDb = doneTask.getTypeId();
                doneTask.setTypeName(workTypeService.selectById(typeIdDb).getName());
            }
            return ReturnBean.ok(doneTasks);
        }

      return ReturnBean.ok();
    }

}
